package com.pengxun.manager.controller.system;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pengxun.manager.service.system.LoginService;
import com.pengxun.manager.vo.LoginVO;
import com.szeastroc.common.exception.EastrocException;
import com.szeastroc.common.result.Result;
import com.szeastroc.common.result.ResultUtils;

@RestController
@RequestMapping("/login")
public class LoginController  {

	@Resource
	private LoginService loginService;
	



	@RequestMapping
	public Result login(@RequestBody LoginVO loginVO) {
		try {
			return ResultUtils.render(loginService.login(loginVO));
		} catch (IllegalArgumentException e) {
			Result result = ResultUtils.renderError(e.getMessage());
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("showValidate", false);
			result.setData(data);
			return result;
		} catch (EastrocException e) {
			Result result = ResultUtils.renderError(e.getMessage());
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("showValidate", true);
			result.setData(data);
			return result;
		} catch (Exception e) {
			return ResultUtils.renderError(e.getMessage());
		}

	}

	/**
	 * 获取当前登录的用户信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/get-current-user")
	public Result getCurrentUser() {
		try {
			return ResultUtils.render(loginService.getCurrentUserVO());
		} catch (EastrocException e) {
			Result result = ResultUtils.renderError(e.getMessage());
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("showValidate", true);
			result.setData(data);
			return result;
		} catch (Exception e) {
			return ResultUtils.renderError(e.getMessage());
		}
	}
}
